package com.sx.gongzhong.calender.weiget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sx.gongzhong.R;
import com.sx.gongzhong.calender.bean.AttrsBean;
import com.sx.gongzhong.calender.bean.DateBean;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MonthView extends ViewGroup {

    private static final int ROW = 6;
    private static final int COLUMN = 7;

    private static final int COLOR_SET = 1;//设置文字颜色
    private int lastMonthDays;//记录当月显示的上个月天数
    private int nextMonthDays;//记录当月显示的下个月天数
    private int currentMonthDays;//记录当月天数
    private Context mContext;

    private AttrsBean mAttrsBean;

    public MonthView(Context context) {
        this(context, null);
    }

    public MonthView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);

        mContext = context;
        setBackgroundColor(Color.WHITE);
    }

    /**
     * @param dates   需要展示的日期数据
     */
    public void setDateList(List<DateBean> dates,int currentMonthDays) {
        if (getChildCount() > 0) {
            removeAllViews();
        }
        int count = 0 ;// 当前月选中的数量

        this.currentMonthDays = currentMonthDays;
        lastMonthDays = 0;
        nextMonthDays = 0;
        for (int i = 0; i < dates.size(); i++) {
            final DateBean date = dates.get(i);

            if (date.getType() == 0) {
                lastMonthDays++;
                addView(new View(mContext), i);
                continue;
            }

            if (date.getType() == 2) {
                nextMonthDays++;
                addView(new View(mContext), i);
                continue;
            }

            View view = LayoutInflater.from(mContext).inflate(R.layout.item_month_layout, null);
            TextView solarDay = (TextView) view.findViewById(R.id.solar_day);
            if (date.getSolar()[2] < 10){
                solarDay.setText("  "+date.getSolar()[2]);
            }else {
                solarDay.setText(String.valueOf(date.getSolar()[2]));
            }

            //默认选中的多个日期
            if (mAttrsBean.getSelectDefaultDates() != null){
                for (int[] d : mAttrsBean.getSelectDefaultDates()) {
                    if (date.getType() == 1 && d[0] == date.getSolar()[0]
                            && d[1] == date.getSolar()[1]
                            && d[2] == date.getSolar()[2]) {
                        count ++;
                        setDayColor(view, COLOR_SET);
                        break;
                    }
                }
            }
            //设置禁用日期
            if (date.getType() == 1) {
                view.setTag(date.getSolar()[2]);
            }
            addView(view, i);
        }
        mAttrsBean.getMonthCount().add(count);
        requestLayout();
    }

    private void setDayColor(View v, int type) {
        ImageView select_bg = (ImageView) v.findViewById(R.id.select_bg);
        if (type == 0) {
            select_bg.setVisibility(GONE);
        } else if (type == 1) {
            select_bg.setVisibility(VISIBLE);
        }
    }
    public void refresh(HashSet<Integer> set) {
        for (Integer day : set) {
            setDayColor(findDestView(day), COLOR_SET);
        }
        invalidate();
    }
    /**
     * 查找要跳转到的页面需要展示的日期View
     *
     * @param day
     * @return
     */
    private View findDestView(int day) {
        View view = null;
        for (int i = lastMonthDays; i < getChildCount() - nextMonthDays; i++) {
            if ((Integer) getChildAt(i).getTag() == day) {
                view = getChildAt(i);
                break;
            }
        }
        if (view == null) {
            view = getChildAt(currentMonthDays + lastMonthDays - 1);
        }

        if ((Integer) view.getTag() == -1) {
            view = null;
        }
        return view;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        int itemWidth = widthSpecSize / COLUMN;
        //计算日历的最大高度
        if (heightSpecSize > itemWidth * ROW) {
            heightSpecSize = itemWidth * ROW;
        }
        setMeasuredDimension(widthSpecSize, heightSpecSize);
        int itemHeight = heightSpecSize / ROW;
        int itemSize = Math.min(itemWidth, itemHeight);
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            childView.measure(MeasureSpec.makeMeasureSpec(itemSize, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(itemSize, MeasureSpec.EXACTLY));
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (getChildCount() == 0) {
            return;
        }
        View childView = getChildAt(0);
        int itemWidth = childView.getMeasuredWidth();
        int itemHeight = childView.getMeasuredHeight();
        //计算列间距
        int dx = (getMeasuredWidth() - itemWidth * COLUMN) / (COLUMN * 2);
        //当显示五行时扩大行间距
        int dy = 0;
        if (getChildCount() == 35) {
            dy = itemHeight / 5;
        }
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);

            int left = i % COLUMN * itemWidth + ((2 * (i % COLUMN) + 1)) * dx;
            int top = i / COLUMN * (itemHeight + dy);
            int right = left + itemWidth;
            int bottom = top + itemHeight;
            view.layout(left, top, right, bottom);
        }
    }
    public void setAttrsBean(AttrsBean attrsBean) {
        mAttrsBean = attrsBean;
    }
}
